package com.xm.controller;

import com.xm.pojo.*;
import com.xm.service.footService;
import com.xm.service.productService;
import com.xm.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Products")
@CrossOrigin
public class productController {

    @Autowired
    private productService productService;

    @Autowired
    private userService userService;

    @Autowired
    private footService footService;

    //商品展示列表(1可传（按需传参）:1.name(商品名),2.categoryId(商品类别)，3.hot（是否热门商品 1热门，0默认）,4.price(商品价格排序[1正，0倒序]))
    @PostMapping("/productInfo")
    public R productInfo(@RequestBody product product){
        Map<String,Object> map=new HashMap<>();
        map.put("priceStatus",product.getPrice());
        List<product> list=productService.productInfo(product);
        map.put("list",list);
        if(list.size()>0){
            return R.OK().errorMsg("商品查询成功!").data("productList",map);
        }else{
            return R.ERROR().errorMsg("暂无商品!");
        }
    }

    //商品分类列表
    @GetMapping("/categoryInfo")
    public R categoryInfo(){
        List<product> list=productService.categoryInfo();
        return R.OK().errorMsg("商品分类列表查询成功!").data("productCategoryInfo",list);
    }

    //单个商品信息(测试ID：992（图）,936（无图），996(无图片)，userId:123484)
    @GetMapping("/productDetail/{userId}/{id}")
    public R productDetail(@PathVariable int userId,@PathVariable int id){
        //商品信息
        product product =productService.products(id);
        //<!--商品选项-->
        List<product>  productDetail=productService.productDetail(id);
        //商品图片
        List<product> images=productService.productImage(id);
        //判断用户是否收藏该商品
        boolean userWish=false;
        product wish=userService.wishlistInfo(userId,id);
        if(wish!=null){
            userWish=true;
        }
        //添加足迹
        foot foot=footService.userFootInfo(userId,id);//查询是否已浏览过该商品
        if(foot!=null){//判断是否存在该商品足迹
            //存在，只修改时间
            footService.updateFoot(userId,id);

        }else{
            //不存在，添加该商品至足迹
            footService.addFoot(userId,id);
        }

        Map map=new HashMap();
        map.put("product",product);
        map.put("productDetail",productDetail);
        map.put("Image",images);
        map.put("userWish",userWish);
        return R.OK().errorMsg("商品信息查询成功!").data(map);
    }



    /*充值金额*/
    @GetMapping("/Accountrecharge")
    public R Accountrecharge(){
        List<account> list=productService.Accountrecharge();
        return R.OK().errorMsg("充值金额").data("list",list);
    }

    /*钱包充值*/
    @PostMapping("/AddAccount/{username}/{amount}")
    public R addAccounts(@PathVariable String username,@PathVariable double amount){
        boolean flag=userService.insertAccount(username,amount,"充值");
        if(flag){
            User user=userService.login(username);
            user.setAccount(new account(userService.remainder(user.getUserName()),userService.recharge(user.getUserName()),userService.consume(user.getUserName())));
            return R.OK().errorMsg("充值成功").data("user",user);
        }else{
            return R.ERROR().errorMsg("充值失败");

        }
    }




}
